package com.example.basedemosynchronize;

/**
 * <p>synchronized关键字的作用：能够保证同一时刻最多只有一个线程执行该段代码，保证并发下任务的安全执行
 * synchronized的用法：第一类用法是当作对象锁来用
 * 第二类用法是当作类锁来用
 * </p>
 *
 * @author:libaoguang
 * @date:Creatrd in 2020/3/25
 * @since:1.0
 */
public class Usage1 implements Runnable {
    /**
     * <p>
     * </p>
     *
     * @Param:
     * @Return:
     **/
    public  synchronized void method1() {
        System.out.println("my name is==="+Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("end==="+Thread.currentThread().getName());
    }

    @Override
    public void run() {
        method1();
    }
}
class T01{
    public static void main(String[] args) {
        Usage1 usage1 = new Usage1();
        Thread thread1=new Thread(usage1);
        Thread thread2=new Thread(usage1);

        thread1.start();
        thread2.start();
    }
}